package com.cogent.business.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.validator.domain.ValidatorDomain;

public class WorkFolwHelper {
	
	public static String getWorkFlowPerfix(HashMap dataMap) {
		String ret = "";
		int status = MapUtil.getIntValue(dataMap, "status");
		if(status == 1)
			ret = "审核";
		else
			ret = "审批";
		
		return ret;
	}
	
	public static String getWorkFlowStatus(HashMap dataMap) {
		int curstate = MapUtil.getIntValue(dataMap, "wf_opstate");
		int status = MapUtil.getIntValue(dataMap, "status");
		int ret = 0;
		if((status == 1) || (status == 3))
			ret = status + curstate;
		else if(status == 4) {
			if(curstate == 2)
				ret =3;
			else
				ret = 2;
		}
		
		return "" + ret;
	}
	
	public static String getWorkFlowState(HashMap dataMap) {
		String ret = "";
		int status = MapUtil.getIntValue(dataMap, "status");
		if(status < 4)
			ret = "2";
		else
			ret = "3";
		
		return ret;
	}
	
	public static void getWorkFlowFormDomain(HashMap dataMap, FormDomain formDomain) {
		if(formDomain.getWorkFlowList() == null) 
			return;
		List ret = new ArrayList();
		Iterator it = formDomain.getWorkFlowList().iterator();
		Map allcolumnMap = formDomain.getColumnMap();
		while(it.hasNext()) {
			FormColumnDomain formcolumndomain = (FormColumnDomain)it.next();
			formcolumndomain.setCn(getWorkFlowPerfix(dataMap) + formcolumndomain.getCn());
			ret.add(formcolumndomain);
			allcolumnMap.put(formcolumndomain.getName(), formcolumndomain);
		}
		formDomain.setWorkFlowList(ret);
		formDomain.setColumnMap(allcolumnMap);
	}
	
	public static List getWorkFlowValidatorList(HashMap dataMap, FormDomain formDomain) {
		if(formDomain.getWorkFlowList() == null) 
			return formDomain.getValidatorList();
		getWorkFlowFormDomain(dataMap, formDomain);
		List ret = new ArrayList();
		Iterator it = formDomain.getValidatorList().iterator();
		while(it.hasNext()) {
			ValidatorDomain validatorDomain = (ValidatorDomain)it.next();
			String keyname = validatorDomain.getProperty();
			FormColumnDomain FormColumnDomain = (FormColumnDomain)formDomain.getColumnMap().get(keyname);
			validatorDomain.setKey(FormColumnDomain.getCn());
			ret.add(validatorDomain);
		}
		
		return ret;
	}
	
	public static void dealWorkFlowBussiness(BussinessDaoFacade bussinessFacade, String id, HashMap dataMap) {
		dealWorkFlowBussiness(bussinessFacade, id, dataMap, "1");
	}
	
	public static void dealWorkFlowBussiness(BussinessDaoFacade bussinessFacade, String id, HashMap dataMap, String optype) {
		HashMap curflowMap = new HashMap();
		curflowMap.put("wfid", id);
		if(MapUtil.isEmpty(dataMap, "wf_opstate")) {
			curflowMap.put("optype", optype);
			curflowMap.put("opdate", JodaDateUtil.getShortDate(MapUtil.getStringValue(dataMap, "modifydate")));
			curflowMap.put("opperson", MapUtil.getStringValue(dataMap, "modifier"));
		} else {
			curflowMap.put("optype", WorkFolwHelper.getWorkFlowState(dataMap));
			curflowMap.put("opstate", MapUtil.getStringValue(dataMap, "wf_opstate"));
			curflowMap.put("opdate", MapUtil.getStringValue(dataMap, "wf_opdate"));
			curflowMap.put("opperson", MapUtil.getStringValue(dataMap, "wf_opperson"));
			curflowMap.put("opresult", MapUtil.getStringValue(dataMap, "wf_opresult"));
		}
		curflowMap.put("credate", MapUtil.getStringValue(dataMap, "modifydate"));
		curflowMap.put("creator", MapUtil.getStringValue(dataMap, "modifier"));
		bussinessFacade.save("wf_record", curflowMap);
	}
}
